package cn.stylefeng.guns.modular.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.storage.entity.StorageAllocationOut;
import cn.stylefeng.guns.modular.storage.model.params.StorageAllocationOutParam;
import cn.stylefeng.guns.modular.storage.service.StorageAllocationOutService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 调拨出库表控制器
 */
@Controller
@RequestMapping("/storageAllocationOut")
public class StorageAllocationOutController extends BaseController {

    private String PREFIX = "/storage/storageAllocationOut";

    @Autowired
    private StorageAllocationOutService storageAllocationOutService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/storageAllocationOut.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/storageAllocationOut_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/storageAllocationOut_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StorageAllocationOutParam storageAllocationOutParam) {
        this.storageAllocationOutService.add(storageAllocationOutParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StorageAllocationOutParam storageAllocationOutParam) {
        this.storageAllocationOutService.update(storageAllocationOutParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StorageAllocationOutParam storageAllocationOutParam) {
        this.storageAllocationOutService.delete(storageAllocationOutParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StorageAllocationOutParam storageAllocationOutParam) {
        StorageAllocationOut detail = this.storageAllocationOutService.getById(storageAllocationOutParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StorageAllocationOutParam storageAllocationOutParam,String condition,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	storageAllocationOutParam.setDeliverId(user.getDeliverId());
    	storageAllocationOutParam.setDeptId(user.getDeptId());
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		storageAllocationOutParam.setAllocationTime("'"+startTime+"' and '"+endTime+"'");
		}
        return this.storageAllocationOutService.findPageBySpec(storageAllocationOutParam,condition,user.getOperateId());
    }

}


